#include "GameOpt.h"

int main(void)
{
	ofstream SavedFile;
	ifstream LoadedFile;
	srand ((unsigned int)time(NULL));	//seed the random mechanism
	string TmpMsg1,TmpMsg2,TmpMsg3;
	Game *MainGame=NULL;
	char TmpStr[3];
	char inCh = 0;
	
	do
	{
		inCh = MainGame->ShowMenu();
		switch (inCh)
		{
		case 'N':
			system("CLS");
			MainGame = new Game;
			break;
		case 'Q':
			delete MainGame;
			return 0;
			break;
		case 'S':
			cout << "Nothing to save at this moment.\n<ENTER> to continue..." << endl;
			WaitForEnter();
			break;
		case 'L':
			try
			{
				LoadedFile.open("DnDgame.dat", ios::in | ios::binary);
				if (!LoadedFile.is_open())
					throw 1;
				MainGame=new Game(true); //dummy game
				if (LoadedFile && MainGame->LoadState(LoadedFile))
				{
					cout << "\nGame loaded successfuly!\n<ENTER> to continue..." << endl;
					WaitForEnter();
				}
				else
					if (MainGame)
						delete MainGame;
				LoadedFile.close();
			}
			catch (int i)
			{
				if (i)
					cout << "Error opening file!\n" << endl;
				WaitForEnter();
			}
			break;
		case 'R':
			break;
		default:
			break;
		}
	}while(MainGame==NULL);

	do
	{
		MainGame->PrintGame("Round Start","<ENTER> Continue","<ESC>   Main Menu"); //option to enter to Menu
//********************Main Menu Processing*************************************
		if(!WaitForEnterOrESC())//if ESC was Pressed
		{
			inCh = MainGame->ShowMenu();
			switch (inCh)
			{
			case 'N':
				system("CLS");
				cout  << "Press <Enter> to Start New Game"<<endl;
				WaitForEnter();
				delete MainGame;
				MainGame = new Game;
				break;
			case 'Q':
				break;
			case 'S':
				try
				{
					SavedFile.open("DnDgame.dat", ios::out | ios::binary);
					if (!SavedFile.is_open())
						throw 1;
					if (SavedFile && MainGame->SaveState(SavedFile))
					{
						cout << "\nGame saved successfuly!\n<ENTER> to continue..." << endl;
						WaitForEnter();
					}
					else
						if (MainGame)
							delete MainGame;
					SavedFile.close();
				}
				catch (int i)
				{
					if (i)
						cout << "Error opening file!\n" << endl;
					WaitForEnter();
				}
				break;
			case 'L':
				try
				{
					LoadedFile.open("DnDgame.dat", ios::in | ios::binary);
					if (!LoadedFile.is_open())
						throw 1;
					if (LoadedFile && MainGame->LoadState(LoadedFile))
					{
						cout << "\nGame loaded successfuly!\n<ENTER> to continue..." << endl;
						WaitForEnter();
					}
					else
						if (MainGame)
							delete MainGame;
					LoadedFile.close();
				}
				catch (int i)
				{
					if (i)
						cout << "Error opening file!\n" << endl;
					WaitForEnter();
				}
				break;
			case 'R':
				break;
			default:
				break;
			}
		}
//********************End of Main Menu Processing******************************
		else
		{
			while(MainGame->RotatePlayers()!=-1)//if there are no more players
			{
				MainGame->PrintGame(MainGame->getPlayer(MainGame->getCurrentPlayer()).getName() + "'s Turn"
					,"<ENTER> Continue");//Break before entering to Turn
				WaitForEnter();
				MainGame->SetMsg("",1);//clear the 2nd line
				do					//doing the player turn until he done
				{
					MainGame->PlayerMove();
				}while(!MainGame->CurrPlayerTurnDone());
				MainGame->getPlayer(MainGame->getCurrentPlayer()).resetMovement();//reset the player turn (inc his movment back)
			};
			
			
			while(MainGame->RotateMonsters()!=-1)
			{
				_itoa_s(MainGame->getCurrentMonster()+1,TmpStr,10);
				MainGame->PrintGame("Monster " +string(TmpStr)+" "+MainGame->getMonster(MainGame->getCurrentMonster()).getcToken()+ " Turn"
					,"<ENTER> Continue");
				WaitForEnter();
				do
				{
					MainGame->MonsterMove();
				}while(!MainGame->CurrMonsterTurnDone());
				MainGame->getMonster( MainGame->getCurrentMonster()).resetMovement();
			};
			#ifdef DBG
			MainGame->AddMonsters(0);
			#else
			MainGame->AddPresents(PRESENT_ADD_EACH_TURN);
			MainGame->AddMonsters(MOSTERS_ADD_EACH_TURN);
			#endif
		}
		if(MainGame->HaveWinner() != "")
		{
			system("CLS");
			cin.clear();
			cout << "The Winner is " << MainGame->HaveWinner() << endl 
				 << "Press <Enter> to Start New Game" << endl;
			WaitForEnter();
			delete MainGame;
			MainGame = new Game;
		}

	}while (inCh != 'Q'); //if the user asked to quit
	delete MainGame;
	return 0;
}
